var username = ''; //widget.preferenceForKey('username');
var isNew = 1;
var current = 'feed0';

Element.observe(window, 'load', function(){
  if(username==null || username==""){
    setUsername();
  }else{
	start(1);  
  }
});

Event.observe(window, 'close', function(){
	window.fade({duration: 2.0});
});

Event.observe(window, 'scroll', function(){
	var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
	    //Netscape compliant
	    scrOfY = window.pageYOffset;
	    scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    //DOM compliant
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    //IE6 standards compliant mode
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
	  }
	( [ scrOfX, scrOfY ]);
    
	if(scrOfY==0){
		$('header').style.position = '';
	}else{
		$('header').style.position = 'fixed';
		$('header').style.top = '0px';
	}
});

function setUsername(){
	if($('login')==undefined){
	  var login = new FFOptionPanel();
	  $('body').insert(login.optionP);
	  $('login').slideDown();
	}else{
	  $('login').slideDown({duration: 2.0});
	}	
};


function start(option){
  try{
	if(option==1 || option==null){
	  ffw = new FriendFeedWidget(username, {num: 15, isNew: 1});
	}else{
	  isNew = isNew +1;	
	  ffw = new FriendFeedWidget(username, {num: Number(option.num), start: Number(option.start), isNew: isNew});
	}
	ffw.getFeeds();
  }catch (e){
	alert(e);
  }  
}

function scrollToNext(){
	$('header').style.position = 'fixed';
	var number = Number(current.substring('feed'.length)) + 1;
	var scrollTo = 'feed'+ number;
	new Effect.ScrollTo($(scrollTo), {duration: 1.0, offset: -50});
	$(scrollTo).highlight({duration: 4.0});
	current = scrollTo;
}

function scrollToPrev(){
	var number = Number(current.substring('feed'.length)) - 1;
	if(number > 0){ 
	  $('header').style.position = 'fixed';	
	  var scrollTo = 'feed'+ number;
	  new Effect.ScrollTo($(scrollTo), {duration: 1.0, offset: -50});
	  $(scrollTo).highlight({duration: 4.0});
	  current = scrollTo;
	}else{
	  $('header').style.position = '';	
	  $('header').scrollTo();
	  current = 'feed0';
	}  
}
